package com.myk.game.gobangchess.ai.shock;

public class Winningtest_push {

    public static int[] winningtest_push(int[][] checkBoard) {
        int[] canWin = {0, 0};

        if (checkBoard[2][3] == 0) {
            checkBoard = flipHorizontal(checkBoard);
        } else if (checkBoard[3][2] == 0) {
            checkBoard = flipVertical(checkBoard);
        } else if (checkBoard[3][3] == 0) {
            checkBoard = flipVertical(flipHorizontal(checkBoard));
        }

        int checkTurn = checkBoard[2][3];

        if (checkBoard[4][2] == 0 && checkBoard[2][4] == 0) {
            if (checkBoard[5][2] == checkTurn || checkBoard[2][5] == checkTurn ||
                checkBoard[1][2] == checkTurn || checkBoard[2][1] == checkTurn) {
                if (checkTurn == 1) {
                    canWin[0] = 1;
                } else {
                    canWin[1] = 1;
                }
            } else if (checkBoard[5][1] == checkTurn || checkBoard[1][5] == checkTurn) {
                if (checkBoard[4][4] != 0) {
                    if (checkTurn == 1) {
                        canWin[0] = 1;
                    } else {
                        canWin[1] = 1;
                    }
                }
            }
        }

        return canWin;
    }

    private static int[][] flipHorizontal(int[][] matrix) {
        int numRows = matrix.length;
        int numCols = matrix[0].length;
        int[][] result = new int[numRows][numCols];

        for (int i = 0; i < numRows; i++) {
            for (int j = 0; j < numCols; j++) {
                result[i][j] = matrix[i][numCols - 1 - j];
            }
        }

        return result;
    }

    private static int[][] flipVertical(int[][] matrix) {
        int numRows = matrix.length;
        int numCols = matrix[0].length;
        int[][] result = new int[numRows][numCols];

        for (int i = 0; i < numRows; i++) {
            for (int j = 0; j < numCols; j++) {
                result[i][j] = matrix[numRows - 1 - i][j];
            }
        }

        return result;
    }
}
